import { DataTypes, Model } from 'sequelize';
import sequelize from '../db';

/**
 * 字典类型模型
 */
class DictionaryType extends Model {
  public id!: string;
  public tenantId!: string | null; // null 表示系统字典
  public code!: string;
  public name!: string;
  public english?: string;
  public description?: string;
  public status!: boolean;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;

  // 关联的字典项
  public items?: DictionaryItem[];
}

DictionaryType.init(
  {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    tenantId: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'tenant_id',
      comment: '租户ID，为null表示系统字典',
    },
    code: {
      type: DataTypes.STRING(100),
      allowNull: false,
      unique: true,
      comment: '字典编码（唯一标识）',
    },
    name: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: '字典名称',
    },
    english: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '英文名称',
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: '描述',
    },
    status: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      comment: '状态（启用/禁用）',
    },
  },
  {
    sequelize,
    tableName: 'dictionary_types',
    underscored: true,
    timestamps: true,
    indexes: [
      {
        fields: ['tenant_id'],
      },
      {
        fields: ['code'],
      },
    ],
  },
);

/**
 * 字典项模型
 */
class DictionaryItem extends Model {
  public id!: string;
  public typeId!: string;
  public label!: string;
  public english?: string;
  public value!: string;
  public orderNum!: number;
  public status!: boolean;
  public description?: string;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;
}

DictionaryItem.init(
  {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    typeId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'type_id',
      comment: '字典类型ID',
    },
    label: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: '字典标签（显示值）',
    },
    english: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '英文标签',
    },
    value: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: '字典值（实际值）',
    },
    orderNum: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      field: 'order_num',
      comment: '排序序号',
    },
    status: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      comment: '状态（启用/禁用）',
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: '描述',
    },
  },
  {
    sequelize,
    tableName: 'dictionary_items',
    underscored: true,
    timestamps: true,
    indexes: [
      {
        fields: ['type_id'],
      },
      {
        fields: ['value'],
      },
      {
        fields: ['order_num'],
      },
    ],
  },
);

// 定义关联关系
DictionaryType.hasMany(DictionaryItem, {
  foreignKey: 'typeId',
  as: 'items',
});

DictionaryItem.belongsTo(DictionaryType, {
  foreignKey: 'typeId',
  as: 'type',
});

export { DictionaryType, DictionaryItem };


